package com.info.zhiduoduo.core.service.wxmessage;

import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.common.configs.CommonConfig;
import com.info.zhiduoduo.common.configs.PayConfig;
import com.info.zhiduoduo.common.configs.ZddUrlConfig;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.constants.crowdsource.CrowdsourceUserType;
import com.info.zhiduoduo.common.constants.email.EmailType;
import com.info.zhiduoduo.common.constants.maker.MakerEntrustTaxVideoState;
import com.info.zhiduoduo.common.constants.project.ProjectType;
import com.info.zhiduoduo.common.utils.ContextUtil;
import com.info.zhiduoduo.common.utils.DateUtil;
import com.info.zhiduoduo.common.utils.NumberUtil;
import com.info.zhiduoduo.core.service.WxCrowdsourceMessageService;
import com.info.zhiduoduo.repository.dto.crowdsource.CrowdsourceProjectItemDto;
import com.info.zhiduoduo.repository.dto.maker.MakerDetailDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.crowdsource.Crowdsource;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceAccount;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceCompanyParty;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProject;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProjectItem;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceTradeRecordItem;
import com.info.zhiduoduo.repository.entity.maker.Maker;
import com.info.zhiduoduo.repository.entity.maker.MakerAccount;
import com.info.zhiduoduo.repository.entity.maker.MakerAuthRecord;
import com.info.zhiduoduo.repository.entity.maker.MakerStudio;
import com.info.zhiduoduo.repository.entity.weixin.WxNotice;
import com.info.zhiduoduo.wxsdk.WxApiService;
import com.info.zhiduoduo.wxsdk.config.WxCrowdsourceConfig;
import com.info.zhiduoduo.wxsdk.domain.app.WxTempleteMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Service("wxCrowdsourceMessageService")
public class WxCrowdsourceMessageServiceImpl implements WxCrowdsourceMessageService {

    @Autowired
    WxApiService wxApiService;

    @Autowired
    WxCrowdsourceConfig wxCrowdsourceConfig;
    @Autowired
    PayConfig payConfig;
    @Autowired
    CommonConfig commonConfig;
    @Autowired
    ZddUrlConfig zddUrlConfig;

    /**
     * 专门测试用的, 使用一个通用模板
     */
    private static final String TEMP_TEST = "BlUjsCpze1iDbeeGwTCovrAdCBa9m7eHMlUkH42Hfcw";

    /**
     * 业务处理结果通知模版
     */
    private static final String TEMP_BIZ_RESULT = "lq6ytBT7xK1-OlPPXmXcz-tb_37wHVspKwuQaonk4EM";
    /**
     * 注册成功通知
     */
    private static final String TEMP_REGISTER_NOTIFY = "Rzjik0yvNgOH6KdmH2eLL_GRH1ZeQHxXVit0mIcTL2E";

    /**
     * 智众包公众号 问卷调查通知
     */
    public static final String TEMP_QUESTIONNAIRE = "Rs95_fw54tuxLtwmVNLZq16TeLvjGQNd84gTNcMGpO0";

    // 用户注册审核通知
    public static final String AUTH_NOTIFY_TEMPLATE = "rfc-acIEynIHLihYsxODAhU6cpHVWyq0w1IoheepK_k";
    // 用户注册成功通知
    public static final String REGISTER_NOTIFY_TEMPLATE = "P_s2UdpLeucmaVvoGwUd3AQZQ4836suw307y0Z2Lz4o";
    // 发票开局成功通知
    public static final String INVOICE_NOTIFY_TEMPLATE = "7g8OmcbCPoOJNLTKXdZ-HduMG8qkOx7NFVF-LzJU3kU";

    ExecutorService executor;

    public WxCrowdsourceMessageServiceImpl() {
        this.executor = Executors.newSingleThreadExecutor();
    }

    /**
     * 发送模板消息,如果直接在当前线程发送会堵塞当前线程,造成服务器与前端交互等待时间太长,所以这里采用线程executor,单独执行
     *
     * @param message
     * @return
     */
    private Future sendMessage(WxTempleteMessage message, String name, Integer type, String templateId, Integer userId) {
        if (type != null) {
            String first = "";
            String remark = "";
            String str = "";

            Map<String, WxTempleteMessage.ValueColor> data = message.getData();
            Iterator<Map.Entry<String, WxTempleteMessage.ValueColor>> iterator = data.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, WxTempleteMessage.ValueColor> next = iterator.next();
                String key = next.getKey();
                if (key.equals("first")) {
                    first = next.getValue().getValue();
                } else if (key.equals("remark")) {
                    remark = next.getValue().getValue();
                } else {
                    str += next.getValue().getValue();
                }
            }
            new WxNotice().setNoticeType(type)
                    .setNoticePeople(name)
                    .setNoticeTitle(first)
                    .setNoticeRemarks(remark)
                    .setNoticeContent(str)
                    .setNoticeTemplate(templateId)
                    .setUserId(userId)
                    .insert();
        }
        return executor.submit(() -> {
            wxApiService.sendTempleteMessage(message, wxCrowdsourceConfig.getAppid(), wxCrowdsourceConfig.getSecret());
        });
    }

    @Override
    public boolean crowdsourceAuthState(int acctid) {

        MakerAccount makerAccount = new MakerAccount().selectById(acctid);
        if (makerAccount == null) return false;

        Maker maker = new Maker().selectById(makerAccount.getUserid());
        if (maker == null) return false;

        MakerAuthRecord record = new MakerAuthRecord().selectOne(
                Condition.create().eq("acctid", acctid).orderBy("id", false).last("limit 1"));

        String remark = "";
        switch (record.getState()) {
            case 1:
                remark = "审核失败";
                break;
            case 0:
                remark = "审核通过";
                break;
            default:
                remark = "";

        }

        String openid = maker.getTaxerOpenid();

        String templateId = AUTH_NOTIFY_TEMPLATE;
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(templateId);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("用户注册审核通知", "#000000"));
        data.put("thing2", WxTempleteMessage.ValueColor.of(makerAccount.getRealname()));
        data.put("time5", WxTempleteMessage.ValueColor.of(DateUtil.getString(maker.getCreatetime())));
        data.put("const4", WxTempleteMessage.ValueColor.of(remark));
        message.setData(data);

        sendMessage(message, makerAccount.getRealname(), EmailType.MAKER, templateId, maker.getUserid());
        return true;
    }

    @Override
    public boolean registerSucceed(String realname, String openid, Integer crowdsourceid) {
/*

		{{first.DATA}}
		会员帐号：{{keyword1.DATA}}
		注册时间：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_REGISTER_NOTIFY);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("自然人已成功注册", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(realname));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        message.setData(data);

        sendMessage(message, realname, EmailType.CROWDSOURCE, TEMP_REGISTER_NOTIFY, crowdsourceid);
        return true;
    }

    @Override
    public boolean notifyCrowdsourceProjectTradeItemPaid(CrowdsourceTradeRecordItem tradeItem) {
        if (tradeItem == null) return false;

        CrowdsourceProject project = new CrowdsourceProject().selectById(tradeItem.getProjectid());
        if (project == null) return false;

        CrowdsourceProjectItem projectItem = new CrowdsourceProjectItem().selectById(tradeItem.getProjectItemId());
        if (projectItem == null) return false;

        Crowdsource crowdsource = new Crowdsource().selectById(projectItem.getUserid());
        if (crowdsource == null) return false;
        CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount().selectOne(
                Condition.create().eq("crowdsourceid", crowdsource.getCrowdsourceid()));
        if (crowdsourceAccount == null) return false;

        String openid = crowdsource.getOpenid();
        Date finishtime = tradeItem.getFinishtime();
        if (finishtime == null) finishtime = new Date();
/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("智众包项目到款通知", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(project.getProjectName()));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(finishtime)));
        data.put("keyword3",
                WxTempleteMessage.ValueColor.of("到账金额" + projectItem.getProjectAmount().setScale(2, RoundingMode.DOWN).toString()));
        data.put("remark", WxTempleteMessage.ValueColor.of("请注意查收,谢谢!"));
        message.setData(data);

        sendMessage(message, crowdsourceAccount.getRealname(), EmailType.CROWDSOURCE, TEMP_BIZ_RESULT, crowdsource.getCrowdsourceid());
        return true;
    }

    @Override
    public boolean notifyCrowdsourceMakerProjectTradeItemPaid(CrowdsourceTradeRecordItem tradeItem) {
        if (tradeItem == null) return false;

        CrowdsourceProject project = new CrowdsourceProject().selectById(tradeItem.getProjectid());
        if (project == null) return false;

        CrowdsourceProjectItem projectItem = new CrowdsourceProjectItem().selectById(tradeItem.getProjectItemId());
        if (projectItem == null) return false;
        if (!NumberUtil.equals(projectItem.getUserType(), CrowdsourceUserType.STUDIO)) {
            return false;
        }

        Maker maker = new Maker().selectById(projectItem.getUserid());
        if (null == maker) return false;
        MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", maker.getUserid()));
        if (null == account) return false;

        String openid = maker.getOpenid();
        Date finishtime = tradeItem.getFinishtime();
        if (finishtime == null) finishtime = new Date();
/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("智众包项目到款通知", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(project.getProjectName()));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(finishtime)));
        data.put("keyword3",
                WxTempleteMessage.ValueColor.of("到账金额" + projectItem.getProjectAmount().setScale(2, RoundingMode.DOWN).toString()));
        data.put("remark", WxTempleteMessage.ValueColor.of("请注意查收,谢谢!"));
        message.setData(data);

        sendMessage(message, account.getRealname(), EmailType.CROWDSOURCE_MAKER, TEMP_BIZ_RESULT, account.getUserid());
        return true;
    }

    @Override
    public boolean notifyCrowdsourceProjectSubmitted(CrowdsourceProjectItemDto projectItem) {
        if (projectItem == null) return false;
        CrowdsourceProject project = new CrowdsourceProject().selectById(projectItem.getProjectid());
        if (project == null) return false;

        Crowdsource crowdsource = new Crowdsource().selectById(projectItem.getUserid());
        if (crowdsource == null) return false;
        CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount().selectOne(
                Condition.create().eq("crowdsourceid", crowdsource.getCrowdsourceid()));
        if (crowdsourceAccount == null) return false;
        Company company = new Company().selectById(project.getCompid());
        if (company == null) return false;

        //判断企业是否是承包模式，是的话取主体公司名称
        String companyName = "";
        if (2 == company.getCrowdsourceType()) {
            //获取主体公司信息
            CrowdsourceCompanyParty party = new CrowdsourceCompanyParty().selectById(project.getPartyId());
            if (null != party) {
                companyName = party.getCompanyName();
            }
        } else {
            companyName = company.getCompname();
        }

		String openid = crowdsource.getOpenid();
		String url = "http://m3.zhduoduo.com/naturalProjectInfo?id=" + projectItem.getId();
		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(url);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of(
                "智众包项目发布通知！" + "\n企业：" + companyName + "\n项目金额：" + projectItem.getProjectAmount().setScale(2, RoundingMode.DOWN).toString(),
                "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(("项目进度更新")));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(projectItem.getCreatetime(), DateUtil.FORMAT_STYLE_6)));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("项目已发布"));
        data.put("remark", WxTempleteMessage.ValueColor.of(""));
        message.setData(data);

        sendMessage(message, crowdsourceAccount.getRealname(), EmailType.CROWDSOURCE, TEMP_BIZ_RESULT, crowdsourceAccount.getCrowdsourceid());
        return true;
    }

    @Override
    public boolean notifyMakerEvaluate(int projectItemId) {
        CrowdsourceProjectItem projectItem = new CrowdsourceProjectItem().selectById(projectItemId);
        if (null == projectItem) return false;
        if (!NumberUtil.equals(projectItem.getUserType(), CrowdsourceUserType.STUDIO)) {
            return false;
        }
        CrowdsourceProject crowdsourceProject = new CrowdsourceProject().selectById(projectItem.getProjectid());
        if (crowdsourceProject == null) return false;

        Maker maker = new Maker().selectById(projectItem.getUserid());
        if (null == maker) return false;
        MakerAccount account = new MakerAccount().selectOne(Condition.create().eq("userid", maker.getUserid()));
        if (null == account) return false;
        //判断创客是否评价过，如果评价过则不发送通知
        if (0 != account.getWhetherEvaluate()) return false;

        String url = String.format(zddUrlConfig.getCrowdsourceEvaluateUrl(), maker.getUserid(), UserType.MAKER_CROWDSOURCE,
                crowdsourceProject.getCompid(), UserType.COMPANY, ProjectType.CROWDSOURCE);
        String openid = maker.getOpenid();
		/*
		标题认证通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		认证详情：{{keyword1.DATA}}
		认证结果：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(url);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("您好，恭喜您交易成功，平台邀请您对本次交易进行评价！", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("交易完成"));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("邀请评价"));
        message.setData(data);
        sendMessage(message, account.getRealname(), EmailType.CROWDSOURCE_MAKER, TEMP_BIZ_RESULT, account.getUserid());
        return true;
    }

    @Override
    public boolean notifyCrowdsourceEvaluate(int projectItemId) {
        CrowdsourceProjectItem projectItem = new CrowdsourceProjectItem().selectById(projectItemId);
        if (null == projectItem) return false;
        if (!NumberUtil.equals(projectItem.getUserType(), CrowdsourceUserType.NATURAL_PERSON)) {
            return false;
        }
        CrowdsourceProject crowdsourceProject = new CrowdsourceProject().selectById(projectItem.getProjectid());
        if (crowdsourceProject == null) return false;

        Crowdsource crowdsource = new Crowdsource().selectById(projectItem.getUserid());
        if (crowdsource == null) return false;
        CrowdsourceAccount account = new CrowdsourceAccount().selectOne(Condition.create().eq("crowdsourceid", projectItem.getUserid()));
        if (null == account) return false;
        //判断创客是否评价过，如果评价过则不发送通知
        if (0 != account.getWhetherEvaluate()) return false;

        String url = String.format(zddUrlConfig.getCrowdsourceEvaluateUrl(), account.getCrowdsourceid(), UserType.CROWDSOURCE,
                crowdsourceProject.getCompid(), UserType.COMPANY, ProjectType.CROWDSOURCE);
        String openid = crowdsource.getOpenid();
		/*
		标题认证通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		认证详情：{{keyword1.DATA}}
		认证结果：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(url);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("您好，恭喜您交易成功，平台邀请您对本次交易进行评价！", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("交易完成"));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("邀请评价"));
        message.setData(data);
        sendMessage(message, account.getRealname(), EmailType.CROWDSOURCE, TEMP_BIZ_RESULT, account.getCrowdsourceid());
        return true;
    }

    @Override
    public boolean sendNotice(String title, String url, String openid) {

		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        message.setUrl(url);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("智众包平台公告", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(title));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("请点击查看详情"));
        message.setData(data);

        sendMessage(message, null, null, TEMP_BIZ_RESULT, null);
        return true;
    }

    @Override
    public boolean sendSurveyNotice(String title, String content, String url, String openid) {
		/*

		模版ID：Rs95_fw54tuxLtwmVNLZq16TeLvjGQNd84gTNcMGpO0
				开发者调用模版消息接口时需提供模版ID
		标题问卷接收成功通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		问卷主题：{{keyword1.DATA}}
		发起时间：{{keyword2.DATA}}
		结束时间：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_QUESTIONNAIRE);
        message.setUrl(url);

        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of(title, "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of(content));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getString(new Date())));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("无"));
        data.put("remark", WxTempleteMessage.ValueColor.of(""));
        message.setData(data);

        sendMessage(message, null, null, TEMP_QUESTIONNAIRE, null);
        return true;
    }

    @Override
    public boolean notifyProjectPayFailed(CrowdsourceTradeRecordItem recordItem) {
        CrowdsourceProject project = new CrowdsourceProject().selectById(recordItem.getProjectid());
        CrowdsourceProjectItem projectItem = new CrowdsourceProjectItem().selectById(recordItem.getProjectItemId());
        if (project == null || projectItem == null) return false;

        Crowdsource crowdsource = new Crowdsource().selectById(projectItem.getUserid());
        if (crowdsource == null) return false;
        CrowdsourceAccount crowdsourceAccount = new CrowdsourceAccount().selectOne(
                Condition.create().eq("crowdsourceid", crowdsource.getCrowdsourceid()));
        if (crowdsourceAccount == null) return false;
        Company company = new Company().selectById(project.getCompid());
        if (company == null) return false;

        String openid = crowdsource.getOpenid();

		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("智众包项目状态变更", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("项目名-" + project.getProjectName()));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("支付失败"));
        data.put("remark", WxTempleteMessage.ValueColor.of(
                "项目金额：" + projectItem.getProjectAmount().setScale(2, RoundingMode.DOWN).toString() + "\n更多详细情况，请联系全国客服电话：17710090008 "));
        message.setData(data);

        sendMessage(message, crowdsourceAccount.getRealname(), EmailType.CROWDSOURCE, TEMP_BIZ_RESULT, crowdsourceAccount.getCrowdsourceid());
        return true;
    }

    @Override
    public boolean userStudioConfirmed(int userid) {

        MakerStudio makerStudio = new MakerStudio().selectById(userid);
        if (makerStudio == null) return false;

        Maker maker = new Maker().selectById(makerStudio.getUserid());
        if (maker == null) return false;
        MakerAccount makerAccount = new MakerAccount().selectOne(Condition.create().eq("userid", maker.getUserid()));
        if (makerAccount == null) return false;
        String openid = maker.getTaxerOpenid();
/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(REGISTER_NOTIFY_TEMPLATE);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("用户注册成功通知", "#000000"));
        data.put("thing19", WxTempleteMessage.ValueColor.of(makerAccount.getRealname() ));
        data.put("thing14", WxTempleteMessage.ValueColor.of(makerStudio.getUsername()));
        message.setData(data);

        sendMessage(message, makerAccount.getRealname(), EmailType.MAKER, REGISTER_NOTIFY_TEMPLATE, makerAccount.getUserid());
        return true;
    }

    @Override
    public boolean examineMakerToGongQing(int acctid) {
        MakerAccount makerAccount = new MakerAccount().selectById(acctid);
        if (makerAccount == null) {
            return false;
        }
        Maker maker = new Maker().selectById(makerAccount.getUserid());
        if (maker == null) {
            return false;
        }
        String openid = maker.getOpenid();
		/*
		标题认证通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		认证详情：{{keyword1.DATA}}
		认证结果：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(commonConfig.getWeiXinCrowdSourceLoginUrl());
        message.setTemplate_id(TEMP_REGISTER_NOTIFY);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("创客实名认证审核通知", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("创客名-" + makerAccount.getRealname()));
        data.put("keyword2", WxTempleteMessage.ValueColor.of("您的实名审核已通过，请点击详情提交身份验证资料"));
        message.setData(data);
        sendMessage(message, makerAccount.getRealname(), EmailType.MAKER, TEMP_REGISTER_NOTIFY, makerAccount.getUserid());
        return true;
    }

    @Override
    public boolean examineMakerToBusinessInfoNotify(int acctid, int flag) {
        MakerAccount makerAccount = new MakerAccount().selectById(acctid);
        if (makerAccount == null) {
            return false;
        }
        Maker maker = new Maker().selectById(makerAccount.getUserid());
        if (maker == null) {
            return false;
        }
        String noticeName = "";
        String reason = "";
        if (1 == flag) {
            noticeName = "创客身份验证审核通知";
            reason = makerAccount.getRejectBusinessRegisterReason();
        } else if (2 == flag) {
            noticeName = "创客身份验证审核通知";
            reason = makerAccount.getGovernmentRejectionReasons();
        } else if (3 == flag) {
            noticeName = "创客实名认证审核通知";
        }

        String openid = maker.getOpenid();
		/*
		标题认证通知
		行业IT科技 - 互联网|电子商务
		详细内容{{first.DATA}}
		认证详情：{{keyword1.DATA}}
		认证结果：{{keyword2.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(commonConfig.getWeiXinCrowdSourceLoginUrl());
        message.setTemplate_id(TEMP_REGISTER_NOTIFY);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of(noticeName, "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("创客名-" + makerAccount.getRealname()));
        if (3 != flag) {
            data.put("keyword2", WxTempleteMessage.ValueColor.of("审核失败,原因-" + reason));
            data.put("remark", WxTempleteMessage.ValueColor.of("请重新提交审核资料,谢谢"));
        } else {
            data.put("keyword2", WxTempleteMessage.ValueColor.of("您的实名审核已通过，请点击详情提交身份验证资料"));
        }
        message.setData(data);

        sendMessage(message, makerAccount.getRealname(), EmailType.MAKER, TEMP_REGISTER_NOTIFY, makerAccount.getUserid());
        return true;
    }

    /**
     * 共青城创客，已实名未上传视频的，每天推送一次微信通知，最多提醒5次
     *
     * @param openid openid
     * @return true
     */
    @Override
    public boolean reminderToSubmitData(String openid, String name, Integer userId) {

		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        message.setUrl(commonConfig.getWeiXinCrowdSourceLoginUrl());
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("智众包平台提示", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("温馨提示"));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("【温馨提示】您提交的平台注册资料已认证通过，请尽快完成视频认证，点击进入。"));
        message.setData(data);

        sendMessage(message, name, EmailType.MAKER, TEMP_BIZ_RESULT, userId);
        return true;
    }

	public boolean notifyWuhuMakerTaxVideo(MakerDetailDto makerDetailDto) {
		if (makerDetailDto == null) return false;
		String openid = makerDetailDto.getOpenid();
		String url = "http://m3.zhduoduo.com/login";

		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/

		/*
		  标题：业务状态变更通知
		  详细内容：工商审核通过
		  变更类型：【工商审核通过】
		  变更结果：***公司的创客**，已在工商部门审核通过，工作室名称为“*******”，预计3个工作日下发执照，请提前与企业沟通做好交易前的准备工作。
		  变更时间：系统时间
		  备注：
		 */

        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setUrl(url);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>(5);
        data.put("first", WxTempleteMessage.ValueColor.of("工商审核通过", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("【工商审核通过】"));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of("请联系客服，协助您操作定税视频的录制及上传。"));
        data.put("remark", WxTempleteMessage.ValueColor.of(""));
        message.setData(data);

        sendMessage(message, makerDetailDto.getRealname(), EmailType.MAKER, TEMP_BIZ_RESULT, makerDetailDto.getUserid());

        return true;
    }

    @Override
    public boolean notifyMakerEntrustTaxVideoRejected(MakerStudio makerStudio) {
        Maker maker = new Maker().selectById(makerStudio.getUserid());
        if (maker == null) return false;
        MakerAccount makerAccount = new MakerAccount().selectOne(Condition.create().eq("userid", makerStudio.getUserid()));
        if (makerAccount == null) return false;

        String authResult;
        String remark = "";
        if (NumberUtil.equals(makerStudio.getEntrustTaxVideoState(), MakerEntrustTaxVideoState.REJECTED)) {
            authResult = "审核失败,原因-" + makerStudio.getEntrustTaxVideoRemark();
            remark = "请重新提交审核资料,谢谢";
        } else {
            return false;
        }

        String openid = maker.getOpenid();
		/*
		{{first.DATA}}
		系统名称：{{keyword1.DATA}}
		完成时间：{{keyword2.DATA}}
		结果描述：{{keyword3.DATA}}
		{{remark.DATA}}
		*/
        WxTempleteMessage message = new WxTempleteMessage();
        message.setTouser(openid);
        message.setTemplate_id(TEMP_BIZ_RESULT);
        Map<String, WxTempleteMessage.ValueColor> data = new HashMap<>();
        data.put("first", WxTempleteMessage.ValueColor.of("创客委托定税视频认证审核通知", "#000000"));
        data.put("keyword1", WxTempleteMessage.ValueColor.of("创客名-" + makerAccount.getRealname()));
        data.put("keyword2", WxTempleteMessage.ValueColor.of(DateUtil.getNowTimeString()));
        data.put("keyword3", WxTempleteMessage.ValueColor.of(authResult));
        data.put("remark", WxTempleteMessage.ValueColor.of(remark));
        message.setData(data);

        sendMessage(message, makerAccount.getRealname(), EmailType.MAKER, TEMP_BIZ_RESULT, makerAccount.getUserid());
        return true;
    }
}
